package com.fenxiangtech.mall.service.impl;

import com.fenxiangtech.mall.exception.ProductMissException;
import com.fenxiangtech.mall.exception.StockShortageException;
import com.fenxiangtech.mall.service.ImageService;
import com.fenxiangtech.mall.dao.ProductDao;
import com.fenxiangtech.mall.dao.ProductPropertyDao;
import com.fenxiangtech.mall.dto.PageData;
import com.fenxiangtech.mall.dto.Result;
import com.fenxiangtech.mall.entity.Image;
import com.fenxiangtech.mall.entity.Product;
import com.fenxiangtech.mall.entity.ProductProperty;
import com.fenxiangtech.mall.service.ProductService;
import com.fenxiangtech.mall.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhangjiashun
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ImageService imageService;

    @Autowired
    protected ProductPropertyDao propertyDao;

    @Override
    public List<Product> listSimpleProductByThemeID(Long themeID) {
        List<Product> products = productDao.listSimpleProductByThemeID(themeID);
        if (products.size() < 1) {
            throw new ProductMissException();
        }
        for (Product product : products) {
            this.prefixMainImgUrl(product);
        }
        return products;
    }

    @Override
    public List<Product> listRecentProduct(Integer count) {
        if (count == null || count < 1 || count > 15) {
            count = 15;
        }
        List<Product> products = productDao.listRecentProduct(count);
        for (Product product : products) {
            this.prefixMainImgUrl(product);
        }
        return products;
    }

    @Override
    public List<Product> listProductByCategoryID(Long categoryID) {
        List<Product> products = productDao.listProductByCategoryID(categoryID);
        if (products == null || products.size() < 1) {
            throw new ProductMissException();
        }
        for (Product product : products) {
            this.prefixMainImgUrl(product);
        }
        return products;
    }

    @Override
    public Product getProductByID(Long id) {
        Product product = productDao.getProductByID(id);
        this.prefixMainImgUrl(product);
        List<Image> images = imageService.listImageByProductID(id);
        product.setImages(images);
        List<ProductProperty> properties = propertyDao.getPropertyByProductID(id);
        product.setProperties(properties);
        return product;
    }

    @Override
    public List<Product> listProductByIDs(List<Long> ids) {
        List<Product> products = productDao.listProductByIDs(ids);
        for (Product product : products) {
            this.prefixMainImgUrl(product);
        }
        return products;
    }

    @Override
    public boolean reduceStock(Long id, Integer count) {
        Integer stock = productDao.getStock(id);
        stock -= count;
        if (stock < 0) {
            throw new StockShortageException("id为" + id + "的商品库存不足");
        }
        int effectedNum = productDao.updateStock(id, stock);
        return effectedNum > 0;
    }

    @Override
    public PageData<Product> listProduct(Integer page, Integer size) {
        page = page < 1 ? 1 : page;
        size = size < 1 ? 1 : size;
        Integer index = (page -1) * size;
        List<Product> products = productDao.listProduct(index, size);
        for (Product product : products) {
            this.prefixMainImgUrl(product);
        }
        Integer total = productDao.countProduct();
        return new PageData<Product>(page, size, total, products);
    }

    @Override
    public Result<String> deleteProduct(Long id) {
        Product product = productDao.getProductByID(id);
        if (product == null) {
            throw new ProductMissException();
        }
        product = new Product();
        product.setId(id);
        product.setDeleteTime(new Date().getTime());
        Integer effectedNum = productDao.updateProduct(product);
        if (effectedNum > 0) {
            return new Result<>(true, "SUCCESS");
        }
        return new Result<>(false, "FAIL");
    }

    /**
     * 给Product中的mainImgUrl添加前缀
     * @param product
     */
    private void prefixMainImgUrl(Product product) {
        if (product.getFrom() == 1) {
            String mainImgUrl = product.getMainImgUrl();
            mainImgUrl = PathUtil.PREFIX_IMG_URL + mainImgUrl;
            product.setMainImgUrl(mainImgUrl);
        }
    }
}
